﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sy2_13
{
    public partial class Form1 : Form
    {
        string specialChar = "~!@#$%^&*()_+|{}<>?-=\\[];:,./";
        public Form1()
        {
            InitializeComponent();
        }

        private int checkPass(string pass)
        {
            if (pass.Length < 8) return 0;

            int[] cType = { 0, 0, 0, 0 };

            foreach (char c in pass)
            {
                if (!Char.IsNumber(c) && !Char.IsUpper(c) && !Char.IsLower(c) && specialChar.IndexOf(c) == -1)
                {
                    return 0;
                }

                if (Char.IsNumber(c)) cType[0] = 1;
                else if (Char.IsUpper(c)) cType[1] = 1;
                else if (Char.IsLower(c)) cType[2] = 1;
                else if (specialChar.IndexOf(c) != -1) cType[3] = 1;
            }

            int typeCount = cType[0] + cType[1] + cType[2] + cType[3];
            return typeCount;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string password = textBox1.Text;
            int strength = checkPass(password);

            label2.BackColor = SystemColors.Control;
            label3.BackColor = SystemColors.Control;
            label4.BackColor = SystemColors.Control;
            label5.BackColor = SystemColors.Control;
            label2.Text = "";
            label3.Text = "";
            label4.Text = "";
            label5.Text = "";

            if (strength == 0)
            {
                string msg = "密码不得小于8位!\n且只能是字母、数字和以下特殊字符的组合:\n" + specialChar + "\n请重新输入!";
                MessageBox.Show(msg, "输入错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                if (strength >= 1)
                {
                    label2.BackColor = Color.Red;
                    label2.Text = "弱";
                }
                if (strength >= 2)
                {
                    label3.BackColor = Color.Orange;
                    label3.Text = "较弱";
                }
                if (strength >= 3)
                {
                    label4.BackColor = Color.Yellow;
                    label4.Text = "较强";
                }
                if (strength == 4)
                {
                    label5.Text = "强";
                    label5.BackColor = Color.Green;
                }

            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            label2.BackColor = SystemColors.Control;
            label3.BackColor = SystemColors.Control;
            label4.BackColor = SystemColors.Control;
            label5.BackColor = SystemColors.Control;
            label2.Text = "";
            label3.Text = "";
            label4.Text = "";
            label5.Text = "";
        }
    }
}
